package com.gzy.net_app.ui.activity;

import android.os.Bundle;
import android.widget.Toast;
import com.gzy.net_app.api.HttpService;
import com.gzy.net_app.application.AppContext;
import com.gzy.net_app.databinding.ActivityGetTextBinding;
import com.gzy.net_app.ui.BaseActivity;
import com.gzy.network.NetworkApi;
import com.gzy.network.observer.BaseObserver;
import com.gzy.network.utils.KLog;
import okhttp3.ResponseBody;

public class GetTextActivity extends BaseActivity<ActivityGetTextBinding> {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding.button.setOnClickListener(view -> getResult());
  }

  private void getResult() {
    NetworkApi.createService(HttpService.class)
        .getCourseInfo()
        .compose(NetworkApi.applySchedulers(new BaseObserver<ResponseBody>() {
          @Override public void onSuccess(ResponseBody responseBody) {
            if (AppContext.doJson(responseBody) != null) {
              binding.textView.setText(AppContext.doJson(responseBody));
            } else {
              Toast.makeText(GetTextActivity.this, "数据为空", Toast.LENGTH_SHORT).show();
            }
          }

          @Override public void onFailure(Throwable e) {
            KLog.e("GetTextActivity", e.toString());
            Toast.makeText(GetTextActivity.this, "访问失败", Toast.LENGTH_SHORT).show();
          }
        }));
  }


  /**
   * 访问网络
   */
  // private void requestNetwork() {
  //   NetworkApi.createService(HttpService.class)
  //       .getWallPaper()
  //       .compose(NetworkApi.applySchedulers(new BaseObserver<WallPaperResponse>() {
  //         @Override
  //         public void onSuccess(WallPaperResponse wallPaperResponse) {
  //           List<WallPaperResponse.ResBean.VerticalBean> vertical = wallPaperResponse.getRes().getVertical();
  //           if (vertical != null && vertical.size() > 0) {
  //             String imgUrl = vertical.get(0).getImg();
  //             Glide.with(GetTextActivity.this).load(imgUrl).into(binding.image);
  //           } else {
  //             Toast.makeText(GetTextActivity.this, "数据为空", Toast.LENGTH_SHORT).show();
  //           }
  //         }
  //
  //         @Override
  //         public void onFailure(Throwable e) {
  //           KLog.e("MainActivity", e.toString());
  //           Toast.makeText(GetTextActivity.this, "访问失败", Toast.LENGTH_SHORT).show();
  //         }
  //       }));
  // }

}